package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.SetMealDishMapper;
import com.sky.mapper.admin.SetMealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealDTO> page=setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void addSetMeal(SetmealDTO setmealDTO) {
        //添加套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.insert(setmeal);

        //添加套餐菜品关系数据
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
        }
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        log.info("删除套餐:{}",ids);
        //查询在售状态的套餐
        List<Setmeal> onSales=setMealMapper.onSale(ids);
        if (!CollectionUtils.isEmpty(onSales)){
            throw new BaseException("套餐正在启售中，不能删除");
        }
        setMealMapper.deleteByIds(ids);
        setMealDishMapper.deleteBySetmealId(ids);
    }

    /**
     * 根据id查询套餐
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDTO getById(Long setmealId) {
        log.info("根据id查询数据：{}" ,setmealId);
       return setMealMapper.getById(setmealId);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        log.info("修改套餐:{}",setmealDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.update(setmeal);
        //修改口味，删除原数据，增加新数据
        Long id = setmeal.getId();
        setMealDishMapper.deleteBySetmealId(Collections.singletonList(id));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     */

    @CacheEvict(value = "SetMeal",allEntries = true)
    @Override
    public void startOrStop(Integer status, Long id) {
        /*Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        setMealMapper.update(setmeal);*/
        setMealMapper.startOrStop(status,id);
    }
}
